<?php
class WJ_Widget_Vmenu extends Engine_Widget_Abstract{
	
	protected $acl;
	protected $authRecord;
	
    public function init(array $config) { 

    	$options['name'] 		= 'Vertical Menu';
    	$options['description'] = 'Verticl navigation widget for sidebars or page blocks.';
    	$options['idbase'] 		= 'vmenublock';
    	$options['wdth'] 		= '';
    	$options['height'] 		= '';
 
    	$this->authRecord = $this->getAuthRecord();
    	$this->acl = $this->getAcl();
    }
    
    public function run() {
    	$theme = new Theme_Layout();
    	
    	$cache = $this->getCache();
    	$cacheName = "vertical_menu";
		if( ($results = $cache->load($cacheName)) === false ) {
	    	$core_menus = new Engine_Db_Table_CoreMenus();
	    	$select = $core_menus->select();
	    	$select->where("menu_type_id = 'menu_vertical'");
	    	$select->order("menu_order");
	    	$results = $core_menus->fetchAll($select);
	    	$cache->save($results, $cacheName);
		}
		$items = '';
    	foreach ($results as $row) {
    		$allowed = false;
    		if ($row['menu_secure']) {
    			$resource = "menu_vertical_" . $row["menu_id"];	
    			if ($this->aclAllowed($resource)) {
    				$allowed = true;
    			}
    		} else {
    			$allowed = true;
    		}
    	 	if ($allowed) {
				$items = $items . $theme->sidebarVmenu_LI($row['menu_link_href'], $row['menu_name']);
			}    	
    	}
    	$widget = $theme->sidebarVmenu($items);
    	$log = $this->getLog();
    	$log->info($widget);
    	return $widget;
    }
    
    protected function aclAllowed($resource) {
		$allowed = false;
	    if (isset($authRecord)) {
			try {
				$allowed = $acl->isAllowed($authRecord->aclRole, $resource);
			} catch (Exception $e) {
				$allowed = false;	
			}
	    }   
	    return $allowed; 	
    }
	
    
    
}

?>